/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commonlib.drivers;

import commonlib.domain.ABPair;
import java.util.*;

/**
 * Aquesta classe implementa un driver per testejar de forma interactiva la
 * classe ABPair.
 *
 * El funcionament es senzill. inicialment es demana entrar un parell qualsevol.
 * Seguidament podrem aplicar totes les funcions oferts per la classe a aquest
 * parell.
 * 
 * @author genis.sole
 */
public class ABPairDriver {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Entrar parell alpha-beta:");
        System.out.print("Entrar alpha: ");
        String alpha = sc.next();
        System.out.print("Entrar beta: ");
        String beta = sc.next();
        
        ABPair p = new ABPair(alpha, beta);
        
        printOptions();

        boolean done = false;
        while(!done) {
            Scanner scOption = new Scanner(System.in);
            switch(scOption.nextInt()) {
            case 0:
                done = true;
                break;
            case 1:
                testGets(p);
                break;
            case 2:
                testSets(p);
                break;
            case 3:
                testEquals(p);
                break;
            case 4:
                testCompareTo(p);
                break;
            case 5:
                testHashCode(p);
                break;
            case 6:
                printOptions();
                break;
            default:
                System.out.println("opcio incorrecta");
                break;
            }
        }
    }

    private static void printOptions() {
        System.out.println("+------------------------+");
        System.out.println("|        OPCIONS         |");
        System.out.println("+------------------------+");
        System.out.println("| 1. consultar parell    |");
        System.out.println("| 2. modificar parell    |");
        System.out.println("| 3. consultar igualtat  |");
        System.out.println("| 4. comparar parell     |");
        System.out.println("| 5. consultar codi hash |");
        System.out.println("| 6. consultar opcions   |");
        System.out.println("| 0. sortir              |");
        System.out.println("+------------------------+");
    }
    
    private static void testGets(ABPair p) {
        System.out.println("alpha = " + p.getAlpha());
        System.out.println("beta = " + p.getBeta());
    }

    private static void testSets(ABPair p) {
        Scanner sc = new Scanner(System.in);
        System.out.print("nova alpha:");
        String alpha = sc.next();
        System.out.print("nova beta:");
        String beta = sc.next();
        
        p.setAlpha(alpha);
        p.setBeta(beta);
    }

    private static void testEquals(ABPair p) {
        Scanner sc = new Scanner(System.in);
        System.out.println("entrar parell per consultar igualtat");
        System.out.print("alpha: ");
        String alpha = sc.next();
        System.out.print("beta: ");
        String beta = sc.next();

        ABPair p2 = new ABPair(alpha, beta);
        
        if (p.equals(p2)) {
            System.out.println("("+ p.getAlpha() + "," + p.getBeta() + ") == ("
                               + p2.getAlpha() + "," + p2.getBeta() + ")");
        } else {
            System.out.println("(" + p.getAlpha() + "," + p.getBeta() + ") != ("
                               + p2.getAlpha() + "," + p2.getBeta() + ")");
        }
    }

    private static void testCompareTo(ABPair p) {
        Scanner sc = new Scanner(System.in);
        System.out.println("entrar parell per comparar");
        System.out.print("alpha: ");
        String alpha = sc.next();
        System.out.print("beta: ");
        String beta = sc.next();

        ABPair p2 = new ABPair(alpha, beta);
        
        int comp = p.compareTo(p2);
        if (comp > 0) {
            System.out.println("resultat = " + comp + " -> (" + p.getAlpha()
                               + "," + p.getBeta() + ") > (" + p2.getAlpha()
                               + "," + p2.getBeta() + ")");
        } else if (comp < 0) {
            System.out.println("resultat = " + comp + " -> (" + p.getAlpha()
                               + "," + p.getBeta() + ") < (" + p2.getAlpha()
                               + "," + p2.getBeta() + ")");
        } else {
            System.out.println("resultat = " + comp + " -> (" + p.getAlpha()
                               + "," + p.getBeta() + ") == (" + p2.getAlpha()
                               + "," + p2.getBeta() + ")");
        }
    }

    private static void testHashCode(ABPair p) {
        System.out.println("codi hash = " + p.hashCode());
    }
}
